package com.example.algorithm.linear.link;

import java.util.Iterator;

/**
 * @program: algorithm
 * @description: 双向链表
 * @packagename: com.example.algorithm.linear.link
 * @author: Jay
 * @date: 2022/04/06 16:21:04
 **/
public class TwoWayLinkList<T> implements Iterable<T> {
    //首节点
    private Node head;

    //尾结点
    private Node last;

    //记录链表的长度
    private int N;



    //节点类
    private class Node {
        //存储数据
        T item;
        //下一个节点
        Node next;
        //上一个节点
        Node pre;

        public Node(T item, Node pre, Node next){
            this.item = item;
            this.next = next;
            this.pre = pre;
        }
    }

    public TwoWayLinkList(){
        //初始化头结点和尾结点
        this.head = new Node(null,null,null);
        this.last = new Node(null,null,null);
        //初始化元素个数
        this.N = 0;
    }

    //清空链表
    public void clear(){
        this.head.next = null;
        this.last = null;
        this.N = 0;
    }

    //获取列表的长度
    public int length(){
        return this.N;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        return N == 0;
    }

    //获取链表的第一个元素
    public T getFirst(){
        if (isEmpty()){
            return null;
        }
        return head.next.item;
    }

    //获取最后一个元素
    public T getLast(){
        if (isEmpty()){
            return null;
        }
        return last.item;
    }
    
    //插入元素
    public void insert(T t){
        //如果链表为空
        if (isEmpty()){
            //创建新节点，让新节点为尾结点，让头节点指向新节点
            Node node = new Node(t, head, null);
            last = node;
            head.next = last;
        }else {
            Node oldLast = last;
            //如果链表不为空
            Node node = new Node(t, oldLast, null);
            oldLast.next = node;
            last = node;
        }

        N++;
    }

    //向指定位置i插入元素t
    public void  insert(int i, T t){

        //找到i位置前一个节点
        Node pre = head;
        for(int index = 0; index <= i-1; index++){
            pre = pre.next;
        }

        //找到当前节点
        Node curr = pre.next;
        //创建新节点
        Node node = new Node(t,pre,curr);
        //让i位置前一个节点的下一个节点为新节点
        pre.next = node;
        //让i位置前一个节点变为新节点
        curr.pre = node;
        //N++
        N++;
    }

    //获取指定位置i处的元素
    public T get(int i){
        Node n = head.next;
        for (int index = 0; index < i; index++) {
            n = n.next;
        }
        return n.item;
    }

    //找到元素t在链表中第一次出现的位置
    public int indexOf(T t){
        Node n = head;
        for (int i = 0; n.next != null; i++){
            n = n.next;
            if (n.item.equals(t)){
                return i;
            }
        }
        return -1;
    }

    //删除位置i的元素，并返回
    public T remove(int i){
        //找到i位置前一个节点
        Node pre = head;
        for(int index = 0; index <= i-1; index++){
            pre = pre.next;
        }
        //找到i位置的节点
        Node curr = pre.next;
        //找到i位置下一个节点
        Node nextNode = curr.next;

        pre.next = nextNode;
        nextNode.pre = pre;

        N--;

        return curr.item;

    }

    @Override
    public Iterator<T> iterator() {
        return new TIterator();
    }

    private class TIterator implements Iterator{

        private Node n;

        public TIterator(){
            this.n = head;
        }

        @Override
        public boolean hasNext() {

            return n.next != null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }

}
